package chunk

import (
	"mig/pkg/digester"
	"mig/pkg/types"
)

type DigestChunk struct {
	digester digester.Digester
	records  []*types.Record
	digest   []byte
}

func NewDigestChunk() *DigestChunk {
	return &DigestChunk{
		digester: digester.NewMD5Digester(),
	}
}

func (c *DigestChunk) Print() {
	for _, record := range c.records {
		record.Print()
	}
}

func (c *DigestChunk) Digest() *types.DigestNode {
	return &types.DigestNode{
		Digest: c.digest,
	}
}

func (c *DigestChunk) Records() []*types.Record {
	return c.records
}

func (c *DigestChunk) AddRecord(record *types.Record) {
	c.records = append(c.records, record)
	c.digester.Update(record.Bytes())
}

func (c *DigestChunk) Finalize() {
	c.digest = c.digester.Finalize()
}
